﻿using Abp.Web.Mvc.Authorization;
using Akso.Authorization;
using Akso.Courses;
using Akso.Web.Controllers;
using Akso.Web.Areas.EduAdminModule.Models.Courses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Akso.Codes;
using Akso.Enums;

namespace Akso.Web.Areas.EduAdminModule.Controllers
{
    [AbpMvcAuthorize(AppPermissions.Pages_EduAdminModule_Courses)]
    public class CoursesController : AbpZeroTemplateControllerBase
    {
        private readonly ICourseAppService _coursesAppService;
        private readonly ICodeAppService _codesAppService;
        public CoursesController(ICourseAppService coursesAppService,ICodeAppService codesAppService)
        {
            _coursesAppService = coursesAppService;
            _codesAppService = codesAppService;
        }
        public ActionResult Index()
        {
            //var codes = _codesAppService.GetAllCodes().Items.Select(p => new ComboboxItemDto(p.CodeKey, p.CodeName)).ToList();
            //codes.Insert(0, new ComboboxItemDto("", ""));
            var model = new CoursesViewModel
            {
                FilterText = Request.QueryString["filterText"],
                //GetCourseTermList = codes
            };
            return View(model);
        }

        [AbpMvcAuthorize(AppPermissions.Pages_EduAdminModule_Courses_Create, AppPermissions.Pages_EduAdminModule_Courses_Edit)]
        public async Task<PartialViewResult> CreateOrEditModal(Guid? id)
        {
            var output = await _coursesAppService.GetObjectForEdit(new NullableIdDto<Guid> { Id = id });

            var termCodeList = _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.COURSE_TERM.ToString()).Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.CodeName)).ToList();
            termCodeList.Insert(0, new ComboboxItemDto("", ""));
            //年级
            var gradeCodeList= _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.COURSE_GRADE.ToString()).Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.CodeName)).ToList();
            gradeCodeList.Insert(0, new ComboboxItemDto("", ""));
            //科目
            var subjectCodeList = _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.COURSE_SUBJECT.ToString()).Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.CodeName)).ToList();
            subjectCodeList.Insert(0, new ComboboxItemDto("", ""));
            //课程所属类型
            var courseSubjectTypeCodeList = _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.COURSE_SUBJECTTYPE.ToString()).Items.Select(p => new ComboboxItemDto(p.Id.ToString(), p.CodeName)).ToList();
            courseSubjectTypeCodeList.Insert(0, new ComboboxItemDto("", ""));

            var _courseTypeList = _codesAppService.GetAllCodesByGroupKey(EnumCollection.EnumCode.COURSE_TYPE.ToString()).Items.OrderBy(x => x.CodeKey).ToList();
            if (id!=null)
            {
                foreach(var item in termCodeList)
                {
                    if (item.Value == output.Course.PeriodGUID.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in gradeCodeList)
                {
                    if (item.Value == output.Course.GradeGUID.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in subjectCodeList)
                {
                    if (item.Value == output.Course.SubjectGUID.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
                foreach (var item in courseSubjectTypeCodeList)
                {
                    if (item.Value == output.Course.SubjectTypeGUID.ToString())
                        item.IsSelected = true;
                    else
                        item.IsSelected = false;
                }
            }

            var viewModel = new CreateOrEditCourseModalViewModel(output);
            viewModel.GetCourseTermList = termCodeList;
            viewModel.GetCourseGradeList = gradeCodeList;
            viewModel.GetCourseSubjectList = subjectCodeList;
            viewModel.GetCourseSubjectTypeList = courseSubjectTypeCodeList;
            viewModel.GetCourseTypeList = _courseTypeList;
            return PartialView("_CreateOrEditModal", viewModel);
        }
    }
}